package com.iot08.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.AccountService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.entity.app.Account;
import com.iot08.common.entity.app.Questions;
import com.iot08.common.response.Response;
import com.iot08.common.utils.UUIDGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.io.IOException;
import java.util.Date;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Weichao
 * @Date: 2024/08/09/16:44
 * @Description:
 */
@Slf4j
@Controller
@RequestMapping("account")
public class AppAccountController extends BaseController {
    @Resource
    private AccountService accountService;


    /**
     *
     * @param page
     * @param imei
     * @param status 用户状态: 0:启用 / 1:禁用
     * @param sortState 排序状态
     * @return
     */
    @ResponseBody
    @RequestMapping("list")
    public ModelAndView list(PageVO page,
                             @RequestParam(value = "imei", required = false) String imei,
                             @RequestParam(value = "email", required = false) String email,
                             @RequestParam(value = "status", required = false) Integer status,
                             @RequestParam(value = "sortState", required = false) Integer sortState) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("account/account_list");
        Page<Account> pages = accountService.list(page, imei, status, sortState);

        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("accounts", pages.getContent());
        mv.addObject("status", status);
        mv.addObject("imei", imei);
        mv.addObject("email", email);
        mv.addObject("pageStr", page.getPageStr());
//        mv.addObject("menuId", getMyBut("account"));
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.addObject("sortState", sortState);
        return mv;
    }
    @ResponseBody
    @RequestMapping("updateForbiddenStatus")
    public Response<Object> updateForbiddenStatus(@RequestParam(value = "id") String id, @RequestParam(value = "forbiddenStatus") String forbiddenStatus, @RequestParam(value = "menuId") String menuId) {
        log.info("启用/禁用 APP 用户 : {} forbiddenStatus:{}", id, forbiddenStatus);
        SysUser user = getSessionUser();
        //检查有无操作按钮权限
        Set<String> butSet = getMyBut(menuId);
        if(!butSet.contains("forbidden")){
            return Response.failedResponse("没有权限");
        }
        accountService.updateForbiddenStatus(id, forbiddenStatus, user, getRemortIP());
        return Response.successResponse();
    }

    @ResponseBody
    @RequestMapping("del")
    public Response<Object> del(@RequestParam(value = "id") String id, @RequestParam(value = "tm") String tm, @RequestParam(value = "menuId") String menuId) {
        log.info("删除 APP 用户 : {} 操作时间:{}", id, tm);
        //检查有无操作按钮权限
        Set<String> butSet = getMyBut(menuId);
        if(!butSet.contains("del")){
            return Response.failedResponse("没有权限");
        }
        return accountService.del(id, getSessionUser(), getRemortIP());
    }

    @ResponseBody
    @RequestMapping("toUpdatePwd")
    public ModelAndView toUpdatePwd(@PathParam("id") String id) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("account/account_pwd");
        mv.addObject("account", accountService.findById(id));
        mv.addObject("butSet", getMyBut(getRequest().getParameter("menuId")));
        return mv;
    }

    @ResponseBody
    @RequestMapping("updatePwd")
    public Response<Object> updatePwd(@RequestParam(value = "id") String id, @RequestParam(value = "pwd") String pwd) {
        log.info("修改 APP 用户密码 : {}", id);
        SysUser user = getSessionUser();
        //检查权限
        if (user.getType() == 1) {
            return Response.failedResponse("没有权限");
        }
        if (accountService.findById(id) == null) {
            return Response.failedResponse("用户不存在");
        }
        if (accountService.findById(id).getForbiddenStatus().equals("1")) {
            return Response.failedResponse("用户已被禁用");
        }
        accountService.updatePwd(id, pwd, user, getRemortIP());
        return Response.successResponse();
    }

    /**
     * 问题类别添加
     */
    @RequestMapping("toAdd")
    public ModelAndView toAdd() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("account/account_add");
        return mv;
    }

    /**
     * App账号添加
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ModelAndView add(Account account) throws IOException {
        //设置 email + phone + password +
        ModelAndView mv = new ModelAndView();
        //检查账号是否已经存在
        if(account.getPhone() != null){
            if(accountService.countWithPhone(account.getPhone()) > 0){
                mv.addObject("msg", "fail");
                mv.setViewName("save_result");
                return mv;
            }
        }
        //检查邮箱是否已经存在
        if(account.getEmail() != null){
            if(accountService.countWithEmail(account.getEmail()) > 0){
                mv.addObject("msg", "fail");
                mv.setViewName("save_result");
                return mv;
            }
        }

        account.setId(UUIDGenerator.generate());
        accountService.add(account);

        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

}
